
package com.city.project.dao.impl;

import com.city.project.dao.IUserDao;
import com.city.project.factory.ConnectionFactory;
import com.city.project.model.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements IUserDao {

    private User createUser (ResultSet rs)throws SQLException {
        User user = new User();
        user.setId(rs.getInt("id"));
        user.setPassword(rs.getString("password"));
        user.setUserName(rs.getString("username"));
        return user;
    }

    @Override
    public List<User> getList()  {
        Connection cn = ConnectionFactory.getConnection();
        List<User> list = new ArrayList<>();
        String sql="select * from user";
        PreparedStatement statement = null;
        try {
            statement = cn.prepareStatement(sql);
            ResultSet rs = statement.executeQuery();
            while (rs.next()) {
                list.add(createUser(rs));
            }
            cn.close();
            statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }

    @Override
    public User getOne(int id)  {
        User user = new User();
        Connection cn = ConnectionFactory.getConnection();
        String sql="select * from user where id=?";
        PreparedStatement statement = null;
        try {
            statement = cn.prepareStatement(sql);
            statement.setInt(1, id);
            ResultSet rs = statement.executeQuery();

            while (rs.next()) {
                user=createUser(rs);
            }
            cn.close();
            statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return user;
    }

    @Override
    public int insert(User user)  {
        int i = 0;
        Connection cn = ConnectionFactory.getConnection();
        String sql="insert into user(username,password) values (?,?)";
        PreparedStatement statement = null;
        try {
            statement = cn.prepareStatement(sql);
            statement.setString(1, user.getUserName());
            statement.setString(2, user.getPassword());
            i = statement.executeUpdate();
            cn.close();
            statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return i;
    }

    @Override
    public int delete(int id)  {
        int i = 0;
        Connection cn = ConnectionFactory.getConnection();
        String sql="delete from user where id=?";
        PreparedStatement statement = null;
        try {
            statement = cn.prepareStatement(sql);
            statement.setInt(1, id);
             i = statement.executeUpdate();
            cn.close();
            statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return i;
    }

    @Override
    public int update(User user)  {
        int i = 0;
        Connection cn = ConnectionFactory.getConnection();
        String sql="update user set username=?,password=? where id=?";
        PreparedStatement statement = null;
        try {
            statement = cn.prepareStatement(sql);
            statement.setString(1, user.getUserName());
            statement.setString(2, user.getPassword());
            statement.setInt(3, user.getId());
            i = statement.executeUpdate();
            cn.close();
            statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return i;
    }

    @Override
    public User getByName(String username)  {
        User user = new User();
        Connection cn = ConnectionFactory.getConnection();
        String sql="select * from user where username=?";
        PreparedStatement statement = null;
        try {
            statement = cn.prepareStatement(sql);
            statement.setString(1, username);
            ResultSet rs = statement.executeQuery();
            while (rs.next()) {
                user=createUser(rs);
            }
            cn.close();
            statement.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;

    }
}
